package whu.cn.whushare.ui.main.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.common.UserManager;
import whu.cn.whushare.ui.common.MainReShowFragment;
import whu.cn.whushare.ui.main.MyTabFavoriteActivity;
import whu.cn.whushare.ui.main.MyTabGroupActivity;
import whu.cn.whushare.ui.main.MyTabRelationActivity;
import whu.cn.whushare.ui.main.MyTabSettingActivity;
import whu.cn.whushare.util.Utils;

public class MyHomeFragment extends MainReShowFragment implements View.OnClickListener {
	@ViewMapping(id = R.id.user_layout)
	private RelativeLayout mUserLayout;
	@ViewMapping(id = R.id.my_info)
	private TextView mInfo;
	@ViewMapping(id = R.id.my_relative)
	private TextView mRelative;
	@ViewMapping(id = R.id.my_collection)
	private TextView mCollection;
	@ViewMapping(id = R.id.my_group)
	private TextView mGroup;
	@ViewMapping(id = R.id.my_recommend)
	private TextView mRecommend;
	@ViewMapping(id = R.id.my_setting)
	private TextView mSetting;
	@ViewMapping(id = R.id.avatar)
	private ImageView mAvatar;
	@ViewMapping(id = R.id.user_name)
	private TextView mUserName;
	@ViewMapping(id = R.id.vip_level)
	private TextView mVipLevel;
    @ViewMapping(id = R.id.my_topic)
    private TextView mMyTopic;

	private UserManager mUserManager;
	private User mUser;

	public static final int LOGIN_REQUEST_CODE = 1;
	public static final int REGISTER_REQUEST_CODE = 1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		initParams();
		View root = inflater.inflate(R.layout.my_home_layout, container, false);
		initViews(root);
		initActions();
		return root;
	}

	private void initParams() {
		mUserManager= UserManager.getInstance(getActivity());
		mUser = mUserManager.getCurrentUser();
		// test todo
		if (mUser.equals(User.Null)) {
			User user = new User();
			user.setId("day");
			user.setPassword("123456");
            mUserManager.login(user);
			mUser = user;
		}





	}

	private void initViews(View root) {
		ViewMappingUtil.mapView(this, root);
		getTopBarWithoutBack(root, R.id.top_bar);

		mTopBar.title.setText(R.string.my_vip);
		setUserLayout();
	}

	private void setUserLayout() {
       if(mUser.getNickName()!=null) {
            mUserName.setText(mUser.getNickName());
       }
       else
           mUserName.setText("昵称");
        ImageWorkerManager.getInstance(getActivity()).loadCommonImage(Utils.getPhotoUrl(mUser.getAvatar()), mAvatar,
				R.drawable.img_vip, R.drawable.img_vip, mAvatar.getWidth(), mAvatar.getHeight());
		mVipLevel.setText(String.format(getString(R.string.vip), mUser.getPermission()));
	}

	private void initActions() {
	   mUserManager = UserManager.getInstance(getActivity());
		//mUserLayout.setOnClickListener(this);
		mInfo.setOnClickListener(this);
		mRecommend.setOnClickListener(this);
		mGroup.setOnClickListener(this);
		mRecommend.setOnClickListener(this);
		mCollection.setOnClickListener(this);
		mSetting.setOnClickListener(this);
		mRelative.setOnClickListener(this);
        mMyTopic.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.user_layout:
			if (!mUserManager.hasLogin()) {
				startActivityForResult(LoginActivity.getStartIntent(getActivity()), LOGIN_REQUEST_CODE);
			} else {
				logout();
			}
			break;
		case R.id.my_info:
			startActivityForResult(MyInfoActivity.getStartIntent(getActivity()), REGISTER_REQUEST_CODE);
			break;
		case R.id.my_relative:
			startActivityForResult(MyTabRelationActivity.getStartIntent(getActivity()), 1);
			break;
		case R.id.my_collection:
			startActivityForResult(MyTabFavoriteActivity.getStartIntent(getActivity()), 1);
			break;
		case R.id.my_group:
			startActivityForResult(MyTabGroupActivity.getStartIntent(getActivity()), 1);
			break;
		case R.id.my_setting:
			startActivityForResult(MyTabSettingActivity.getStartIntent(getActivity()), 1);
			break;
        case R.id.my_recommend:
            startActivityForResult(MyDynamicActivity.getStartIntent(getActivity()),1);
		    break;
        case R.id.my_topic:
            startActivityForResult(MyAllTopic.getStartIntent(getActivity()),1);
            break;
            default:
			break;
		}
	}

	private void logout() {
		mUserManager.logout();
		mUser = mUserManager.getCurrentUser();
		toast(R.string.has_login_out);
		setUserLayout();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == LOGIN_REQUEST_CODE) {
			mUser = mUserManager.getCurrentUser();
			setUserLayout();
		}
	}

}
